package cn.com.headfree.server.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gta.edu.sdk.common.dto.ResultData;

import cn.com.headfree.server.entity.TZuulServerInfo;
import cn.com.headfree.server.service.IServerInfoService;

@CrossOrigin("*")
@RestController
@RequestMapping(value="/serverinfo",produces = "application/json;charset=UTF-8")
public class ServerInfoController {
	private static final Logger log = LogManager.getLogger(ServerInfoController.class);
	
	@Autowired
	private IServerInfoService serverInfoService;

	/**
	 * @description 保存路由设置中的微应用信息
	 * @author 冯亚
	 * @date 2020年1月19日 下午2:49:32
	 * @param name
	 * @param request
	 * @return
	 * @return
	 * ResultData
	 */
	@RequestMapping(value = "/save",method=RequestMethod.POST)
	public ResultData save(@RequestParam(required = false)String name,HttpServletRequest request) {
		ResultData result = new ResultData();
		try {
			TZuulServerInfo info = new TZuulServerInfo();
			info.setDelSign(0);
			info.setName(name);
			info.setUpdateTime(new Date());
			info.setUpdateIp(request.getRemoteAddr());
			this.serverInfoService.save(info);
		} catch (Exception e) {
			log.error("保存路由微应用信息出现了错误！", e);
			result.setSuccess(false);
			result.setErrorMsg("保存路由微应用信息出现了异常！");
		}
		return result;
	}
	
	/***
	 * @description 更新路由设置中的微应用信息
	 * @author 冯亚
	 * @date 2020年1月19日 下午2:57:16
	 * @param name
	 * @param id
	 * @param request
	 * @return
	 * @return
	 * ResultData
	 */
	@RequestMapping(value = "/update",method=RequestMethod.POST)
	public ResultData update(@RequestParam(required = true) String name,@RequestParam(required = true) String id,HttpServletRequest request) {
		ResultData result = new ResultData();
		try {
			TZuulServerInfo info = this.serverInfoService.get(TZuulServerInfo.class, id);
			if (info != null) {
				info.setName(name);
				info.setUpdateTime(new Date());
				info.setUpdateIp(request.getRemoteAddr());
				this.serverInfoService.update(info);
			} else {
				log.error("更新路由中微应用信息找不到对应的ID记录！");
				result.setSuccess(false);
				result.setErrorMsg("更新路由中微应用信息找不到对应的ID记录！");
			}
		} catch (Exception e) {
			log.error("更新路由微应用信息出现了错误！", e);
			result.setSuccess(false);
			result.setErrorMsg("更新路由微应用信息出现了异常！");
		}
		return result;
	}
	
	/***
	 * @description 删除路由设置中的微应用
	 * @author 冯亚
	 * @date 2020年1月19日 下午3:01:32
	 * @param id
	 * @return
	 * @return
	 * ResultData
	 */
	@RequestMapping(value = "/delete",method=RequestMethod.GET)
	public ResultData delete(@RequestParam(required = true) String id) {
		ResultData result = new ResultData();
		try {
			this.serverInfoService.delete(TZuulServerInfo.class, id);
		} catch (Exception e) {
			log.error("删除路由微应用信息出现了错误！", e);
			result.setSuccess(false);
			result.setErrorMsg("删除路由微应用信息出现了异常！");
		}
		return result;
	}
	
	/***
	 * @description 查询路由设置中的微应用
	 * @author 冯亚
	 * @date 2020年1月19日 下午3:05:11
	 * @return
	 * @return
	 * ResultData
	 */
	@RequestMapping(value = "/findAll",method=RequestMethod.GET)
	public ResultData findAll() {
		ResultData result = new ResultData();
		try {
			List<TZuulServerInfo> list = this.serverInfoService.findAll();
			result.setData(list);
		} catch (Exception e) {
			log.error("查询路由微应用信息出现了错误！", e);
			result.setSuccess(false);
			result.setErrorMsg("查询路由微应用信息出现了异常！");
		}
		return result;
	}
}
